package dialogs;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JSplitPane;
import java.awt.CardLayout;
import java.util.ArrayList;

public class DetailDialog extends JDialog {
	private JPanel pane = null;
	private JPanel controlPanel = null;
	private JPanel buttonPanel = null;
	private JPanel rightPanel = null;
	private JScrollPane textAreaScrollPane = null;
	private JScrollPane topicScrollPane = null;
	private JTable topicTable = null;
	private JTextArea textArea = null;
	private JButton closeButton = null;
	private JCheckBox lineWrap = null;
	private JCheckBox separator = null;
	public DetailDialogTableModel topicTableModel = null; // @jve:decl-index=0:visual-constraint=""
	private int selectedIndex = -1;
	private boolean isTabSeparated = true;
	private boolean isShowingTypes = false;
	private final String[] titles = { "All of this database's SQL" + " keywords that are <b>NOT</b> also SQL92 keywords.",
			"Math functions available with this database.", "String functions available with this database.",
			"System functions available with this database.", "The time and date functions available with this database", "5", "6", "7", "8" };
	private JSplitPane splitPane = null;
	private JPanel detailPanel = null;
	private JLabel titleLabel = null;
	private JScrollPane typesScrollPane = null;
	private JTable typesTable = null;
	private CardLayout layout = null;
	public KeyValueTableModel typesTableModel = null; // @jve:decl-index=0:visual-constraint=""

	/**
	 * This is the default constructor
	 */
	public DetailDialog() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("DBMS Detail"); // Generated
		this.setSize(450, 375); // Generated
		this.setModal(true); // Generated
		this.setVisible(false); // Generated
		this.setContentPane(getPane()); // Generated
	}

	/**
	 * This method initializes pane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; // Generated
			gridBagConstraints.weighty = 10.0; // Generated
			gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 11); // Generated
			gridBagConstraints.weightx = 10.0; // Generated
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0; // Generated
			gridBagConstraints1.gridwidth = 2; // Generated
			gridBagConstraints1.anchor = java.awt.GridBagConstraints.CENTER; // Generated
			gridBagConstraints1.insets = new java.awt.Insets(0, 0, 0, 11); // Generated
			gridBagConstraints1.gridy = 1; // Generated
			pane = new JPanel();
			pane.setLayout(new GridBagLayout()); // Generated
			pane.add(getSplitPane(), gridBagConstraints); // Generated
			pane.add(getButtonPanel(), gridBagConstraints1); // Generated
		}
		return pane;
	}

	/**
	 * This method initializes scrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTextAreaScrollPane() {
		if (textAreaScrollPane == null) {
			textAreaScrollPane = new JScrollPane();
			textAreaScrollPane.setViewportView(getTextArea()); // Generated
		}
		return textAreaScrollPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			buttonPanel.add(getCloseButton(), null); // Generated
		}
		return buttonPanel;
	}

	/**
	 * This method initializes textArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setLineWrap(true);
			textArea.setAutoscrolls(false);
		}
		return textArea;
	}

	/**
	 * This method initializes closeButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setMnemonic(java.awt.event.KeyEvent.VK_C); // Generated
			closeButton.setText("Close"); // Generated
			closeButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DetailDialog.this.setVisible(false);
				}
			});
		}
		return closeButton;
	}

	/**
	 * This method initializes listScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTopicScrollPane() {
		if (topicScrollPane == null) {
			topicScrollPane = new JScrollPane();
			topicScrollPane.setMinimumSize(new java.awt.Dimension(135, 22)); // Generated
			topicScrollPane.setBorder(BorderFactory.createTitledBorder(null, "Topics", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), Color.black)); // Generated
			topicScrollPane.setViewportView(getTopicTable()); // Generated
		}
		return topicScrollPane;
	}

	/**
	 * This method initializes detailPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getRightPanel() {
		if (rightPanel == null) {
			rightPanel = new JPanel();
			layout = new CardLayout();
			rightPanel.setLayout(layout); // Generated
			rightPanel.setBorder(BorderFactory.createTitledBorder(null, "Details", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION,
					null, null)); // Generated
			rightPanel.add(getDetailPanel(), getDetailPanel().getName()); // Generated
			rightPanel.add(getTypesScrollPane(), getTypesScrollPane().getName()); // Generated
		}
		return rightPanel;
	}

	/**
	 * This method initializes topicTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTopicTable() {
		if (topicTable == null) {
			topicTable = new JTable();
			topicTable.setShowGrid(false); // Generated
			topicTable.setBackground(new java.awt.Color(204, 204, 204)); // Generated
			topicTable.setRowHeight(32); // Generated
			topicTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); // Generated
			topicTable.setModel(getTopicTableModel()); // Generated
			topicTable.getSelectionModel().addListSelectionListener(new TopicSelectionHandler());
		}
		return topicTable;
	}

	private void setTextAreaText(int index) {
		String text = (String) ((Info) topicTableModel.data.get(index)).value;
		if (true == this.isTabSeparated) {
			StringBuffer buf = new StringBuffer(text);
			for (int i = 0; i < buf.length(); i++)
				if (',' == buf.charAt(i))
					buf.setCharAt(i, '\t');
			text = buf.toString();
		}
		this.textArea.setText(text);
	}

	/**
	 * This method initializes topicTableModel
	 * 
	 * @return dialogs.DetailDialogTableModel
	 */
	private DetailDialogTableModel getTopicTableModel() {
		if (topicTableModel == null) {
			topicTableModel = new DetailDialogTableModel();
		}
		return topicTableModel;
	}

	/**
	 * This method initializes checkBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getLineWrap() {
		if (lineWrap == null) {
			lineWrap = new JCheckBox();
			lineWrap.setText("Line Wrap"); // Generated
			lineWrap.setSelected(true); // Generated
			lineWrap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					textArea.setLineWrap(lineWrap.isSelected());
				}
			});
		}
		return lineWrap;
	}

	/**
	 * This method initializes splitPane
	 * 
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setDividerSize(8); // Generated
			splitPane.setLeftComponent(getTopicScrollPane()); // Generated
			splitPane.setRightComponent(getRightPanel()); // Generated
		}
		return splitPane;
	}

	/**
	 * This method initializes detailPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getDetailPanel() {
		if (detailPanel == null) {
			titleLabel = new JLabel();
			titleLabel.setFont(new Font("Dialog", Font.PLAIN, 12)); // Generated
			titleLabel.setText("Select a topic."); // Generated
			detailPanel = new JPanel();
			detailPanel.setLayout(new BorderLayout()); // Generated
			detailPanel.setName("detail"); // Generated
			detailPanel.add(titleLabel, java.awt.BorderLayout.NORTH); // Generated
			detailPanel.add(getTextAreaScrollPane(), java.awt.BorderLayout.CENTER); // Generated
			detailPanel.add(getControlPanel(), java.awt.BorderLayout.SOUTH); // Generated
		}
		return detailPanel;
	}

	/**
	 * This method initializes typesScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTypesScrollPane() {
		if (typesScrollPane == null) {
			typesScrollPane = new JScrollPane();
			typesScrollPane.setName("types"); // Generated
			typesScrollPane.setViewportView(getTypesTable()); // Generated
		}
		return typesScrollPane;
	}

	/**
	 * This method initializes typesTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTypesTable() {
		if (typesTable == null) {
			typesTable = new JTable();
			typesTable.setModel(getTypesTableModel()); // Generated
		}
		return typesTable;
	}

	/**
	 * This method initializes typesTableModel
	 * 
	 * @return dialogs.KeyValueTableModel
	 */
	private KeyValueTableModel getTypesTableModel() {
		if (typesTableModel == null) {
			typesTableModel = new KeyValueTableModel();
		}
		return typesTableModel;
	}

	/**
	 * This method initializes separator
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getSeparator() {
		if (separator == null) {
			separator = new JCheckBox();
			separator.setText("Tab-separated"); // Generated
			separator.setSelected(true); // Generated
			separator.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					isTabSeparated = separator.isSelected();
					setTextAreaText(selectedIndex);
				}
			});
		}
		return separator;
	}

	/**
	 * This method initializes controlPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getControlPanel() {
		if (controlPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1); // Generated
			controlPanel = new JPanel();
			controlPanel.setLayout(gridLayout); // Generated
			controlPanel.add(getSeparator(), null); // Generated
			controlPanel.add(getLineWrap(), null); // Generated
		}
		return controlPanel;
	}

	public void showDBMSDetail(Point loc, Dimension size) {
		this.isShowingTypes = false;
		this.setTitle("DBMS Detail");
		this.layout.first(this.getRightPanel());
		this.setProperLocation(loc, size);
	}

	public void showDBMSTypes(Point loc, Dimension size) {
		this.isShowingTypes = true;
		this.setTitle("DBMS Types");
		this.layout.last(this.getRightPanel());
		this.setProperLocation(loc, size);
	}

	private void setProperLocation(Point loc, Dimension size) {
		loc.x += (size.getWidth() - this.getWidth()) / 2;
		loc.y += (size.getHeight() - this.getHeight()) / 2;
		this.setLocation(loc);
		this.setSize(450, 375);
		this.topicTableModel.fireTableDataChanged();
		this.setVisible(true);
	}

	class TopicSelectionHandler implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			selectedIndex = topicTable.getSelectedRow();
			if (-1 == selectedIndex)
				return;
			if (isShowingTypes) {
				typesTableModel.data = (ArrayList) ((Info) topicTableModel.data.get(selectedIndex)).value;
				typesTableModel.fireTableDataChanged();
			} else {
				setTextAreaText(selectedIndex);
				titleLabel.setText("<html><body>" + titles[selectedIndex] + "</body></html>");
			}
		}
	}
}
